<?php 
Class formsController extends appController
{

	public function index()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$sort=array();
		$art_list=Model('forms')->forms_list($page,$sort);
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		$this->display('index.html');
	}
	public function log()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$id=intval($_GET[id]);
		$sort=array();
		$art_list=Model('forms')->log_list($id,$page,$sort);
		$this->assign('art_list',$art_list['data']);
		$this->assign('art_page',$art_list['pagelink']);
		$this->display('log.html');
	}
	public function add_form()
	{
		$field_list=Model('forms')->get_all_field();
		$this->assign('action','new_form');
		$this->assign('field_list',$field_list);
		$this->display('forms.html');
	}
	public function new_form()
	{
		$result=array('error'=>0,'content'=>'');
		$info['name']=isset($_POST['title'])?$_POST['title']:'';
		$info['submit_inerval']=isset($_POST['interval'])?$_POST['interval']:'';
		$info['stime']=isset($_POST['stime'])?strtotime($_POST['stime']):'';
		$info['etime']=isset($_POST['etime'])?strtotime($_POST['etime']):'';
		$info['fields']=isset($_POST['fields'])?implode('::',$_POST['fields']):'';
	
		$res=Model('forms')->insert($info,'forms');
		
		if($res)
		{
			$opt['id']=$res;
			$opt['fields']=$info['fields'];
			$res=Model('forms')->create_forms($opt);
		}
		else 
		{
			
		$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	
		
	}
	public function edit_form()
	{
		$field_list=Model('forms')->get_all_field();
		$this->assign('action','update_form');
		$this->assign('field_list',$field_list);
		$this->display('forms.html');
	}
	public function update_form()
	{
	
	}
	public function field_list()
	{
		$page=isset($_REQUEST['page'])?intval($_REQUEST['page']):1;
		$sort=array();
		$art_list=Model('forms')->get_field_list($page,$sort);
		$this->assign('art_list',$art_list['result']);
		$this->assign('art_page',$art_list['page']);
		$this->display('field_list.html');
	}
	public function edit_field()
	{
		$id=intval($_GET[id]);
		$art=Model('forms')->get_field($id);
		$this->assign('art',$art);
		$this->assign('id',$id);
		$this->assign('action','update_field');
		$this->display('field.html');
	}
	public function update_field()
	{
		$result=array('error'=>1,'content'=>'');
		$info['data']=isset($_POST['data'])?$_POST['data']:'';
		$info['type']=isset($_POST['type'])?$_POST['type']:'';
		$en_name=isset($_POST['en_name'])?$_POST['en_name']:'';
		$info['name']=isset($_POST['name'])?CH_SC.$_POST['name'].DIG_EC.EN_SC.$en_name.DIG_EC:'';
		$en_tips=isset($_POST['en_tips'])?$_POST['en_tips']:'';
		$info['tips']=isset($_POST['tips'])?CH_SC.$_POST['tips'].DIG_EC.EN_SC.$en_tips.DIG_EC:'';
		$en_default=isset($_POST['en_default'])?$_POST['en_default']:'';
		$info['default_val']=isset($_POST['default'])?CH_SC.$_POST['default'].DIG_EC.EN_SC.$en_default.DIG_EC:'';
		$en_field=is_array($_POST['en_field'])?implode('::',$_POST['en_field']):$_POST['en_field'];
		$info['field']=is_array($_POST['field'])?CH_SC.implode('::',$_POST['field']).DIG_EC.EN_SC.$en_field.DIG_EC:CH_SC.$_POST['field'].DIG_EC.EN_SC.$en_field.DIG_EC;
	     $id=intval($_POST['id']);
		if($info['data']==''||hy_check($info['data'],'un')=='#@_error')
		{
			$result['content']='数据库字段不合规范';
			$this->showMsg($result);
		}
	
		$res=Model('forms')->update($info,array('table'=>'fields','id'=>$id));
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_EDIT');
		}
		else
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	public function new_field()
	{
		$result=array('error'=>1,'content'=>'');
		$info['data']=isset($_POST['data'])?$_POST['data']:'';
		$info['type']=isset($_POST['type'])?$_POST['type']:'';
		$en_name=isset($_POST['en_name'])?$_POST['en_name']:'';
		$info['name']=isset($_POST['name'])?CH_SC.$_POST['name'].DIG_EC.EN_SC.$en_name.DIG_EC:'';
		$en_tips=isset($_POST['en_tips'])?$_POST['en_tips']:'';
		$info['tips']=isset($_POST['tips'])?CH_SC.$_POST['tips'].DIG_EC.EN_SC.$en_tips.DIG_EC:'';
		$en_default=isset($_POST['en_default'])?$_POST['en_default']:'';
		$info['default_val']=isset($_POST['default'])?CH_SC.$_POST['default'].DIG_EC.EN_SC.$en_default.DIG_EC:'';
		$en_field=is_array($_POST['en_field'])?implode('::',$_POST['en_field']):$_POST['en_field'];
		$info['field']=is_array($_POST['field'])?CH_SC.implode('::',$_POST['field']).DIG_EC.EN_SC.$en_field.DIG_EC:CH_SC.$_POST['field'].DIG_EC.EN_SC.$en_field.DIG_EC;
		
		if($info['data']==''||hy_check($info['data'],'un')=='#@_error')
		{
			$result['content']='数据库字段不合规范';
			$this->showMsg($result);
		}
		
		
		$res=Model('forms')->insert($info,'fields');
		if($res)
		{
			$result['error']=0;
			$result['content']=Lang('SUCCESS_ADD');
		}
		else 
		{
			$result['content']=Lang('INVALID_OPERATION');
		}
		$this->showMsg($result);
	}
	public function add_field()
	{
		$this->assign('action','new_field');
		$this->display('field.html');
	}
	public function file()
	{
		$file=current($_FILES);
		$result=array('error'=>0,'url'=>$file['name']);
		$thumb_url=Model('image')->upload_image($file, 'forms/'.date('Ym'));
		$result['url']=$thumb_url;
	
		$result['message']='';
		print_r(json_encode($result));
		exit();
	}
}